package cn.kgc.controller;

import cn.kgc.dto.Dto;
import cn.kgc.feignclient.UserFeignClient;
import cn.kgc.pojo.User;
import cn.kgc.util.DtoUtil;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author yaozhaobao
 * @Date 2020/12/13 11:04
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserFeignClient userFeignClient;


    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public Dto login(@RequestParam String userCode, @RequestParam String pwd) {
        try {
            if (userFeignClient.loginByNP(userCode, pwd)) {
                return DtoUtil.returnSuccess("登录成功");
            } else {
                return DtoUtil.returnFail("登录失败", "100012");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail(e.getMessage(),"100014");
        }
    }





    @RequestMapping(value = "/login2", method = RequestMethod.GET)
    public Dto login2(@RequestParam String userCode, @RequestParam String pwd) {
        Map<String,String> map =new HashMap<String,String>();
        map.put("userCode",userCode);
        map.put("pwd",pwd);
        if (userFeignClient.loginByNP2(map)) {
            return DtoUtil.returnSuccess("登录成功");
        } else {
            return DtoUtil.returnFail("用户名密码有误,登录失败", "100012");
        }
    }


    @RequestMapping(value = "/login3", method = RequestMethod.GET)
    public Dto login3(@RequestParam String userCode, @RequestParam String pwd) {
        User user = new User();
        user.setUserCode(userCode);
        user.setUserPassword(pwd);
        if (userFeignClient.loginByNP3(user)) {
            return DtoUtil.returnSuccess("登录成功");
        } else {
            return DtoUtil.returnFail("用户名密码有误,登录失败", "100012");
        }
    }


    @PostMapping(value = "/addUser")
    public Dto addUser(@RequestBody User user){
       Integer rs= userFeignClient.addUser(user);
       if(rs>0){
           return DtoUtil.returnSuccess("添加用户成功");
       }else{
           return DtoUtil.returnFail("添加数据失败","1000013");
       }

    }

    @GetMapping(value = "/adduser2")
    public Dto addUser(){
       for (int i=0;i<1000;i++){
           userFeignClient.testRibbon(i);
       } ;
       return DtoUtil.returnSuccess();
    }




}